package com.xiaoyu.client.adapter.forum;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.forum.ForumParam;
import com.xiaoyu.commonlib.ui.OnItemRecyClickListener;
import com.xiaoyu.commonlib.utils.MediaUtils;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCUtils;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by jk on 2018/10/18.
 */
public class ForumRecyAdapter extends RecyclerView.Adapter<ForumRecyAdapter.ViewHolder> {
    private Context mContext;
    private List<ForumParam> mList = new ArrayList<>();
    private OnItemRecyClickListener mListener;

    public ForumRecyAdapter(Context mContext, List<ForumParam> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_forum, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        if (TextUtils.isEmpty(mList.get(position).getFourmtitle())) {
            holder.titleTxt.setVisibility(View.GONE);
        } else {
            holder.titleTxt.setVisibility(View.VISIBLE);
            holder.titleTxt.setText(mList.get(position).getFourmtitle());
        }
        holder.scanTxt.setText("浏览 " + mList.get(position).getFourmsum());
        holder.replyTxt.setText("回帖 " + mList.get(position).getFourmReplayNum());
        holder.timeTxt.setText(mList.get(position).getFourmtime());
        holder.videoImgLayout.setVisibility(View.VISIBLE);
        if (mList.get(position).getBig() == 1) {//只有文字
            holder.videoImgLayout.setVisibility(View.GONE);
        } else if (mList.get(position).getBig() == 2) {//一张大图

            holder.bigImg.setVisibility(View.VISIBLE);
            holder.videoPlayer.setVisibility(View.GONE);
            holder.smallImgLayout.setVisibility(View.GONE);
            Glide.with(mContext).load(mList.get(position).getFourmimg().get(0)).into(holder.bigImg);
        } else if (mList.get(position).getBig() == 3) {//小图
            holder.bigImg.setVisibility(View.GONE);
            holder.videoPlayer.setVisibility(View.GONE);
            holder.smallImgLayout.setVisibility(View.VISIBLE);
            for (int i = 0; i < mList.get(position).getFourmimg().size(); i++) {
                if (i < holder.videoImgLayout.getChildCount()) {
                    Glide.with(mContext)
                            .load(mList.get(position).getFourmimg().get(i))
                            .into((ImageView) holder.smallImgLayout.getChildAt(i));
                }
            }
        } else if (mList.get(position).getBig() == 4) {//视频
            holder.videoPlayer.setVisibility(View.VISIBLE);
            holder.bigImg.setVisibility(View.GONE);
            holder.smallImgLayout.setVisibility(View.GONE);
            String videoUrl = mList.get(position).getFourmvideo();
            boolean setUp = holder.videoPlayer.setUp(videoUrl, JCVideoPlayer.SCREEN_LAYOUT_LIST, "");
            if (setUp) {
                MediaUtils.getImageForVideo(videoUrl, new MediaUtils.OnLoadVideoImageListener() {
                    @Override
                    public void onLoadImage(Bitmap bitmap) {
                        holder.videoPlayer.thumbImageView.setImageBitmap(bitmap);
//                        Glide.with(mContext).load(mList.get(position).getVideoimg()).into(holder.videoPlayer.thumbImageView);
                    }
                });
            }

            holder.videoPlayer.tinyBackImageView.setVisibility(View.INVISIBLE);
            holder.videoPlayer.backButton.setVisibility(View.INVISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onItemRecyClick(holder.itemView, position);
            }
        });

    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView titleTxt;
        private RelativeLayout videoImgLayout;
        private LinearLayout smallImgLayout;
        private ImageView bigImg;
        private TextView scanTxt;
        private TextView replyTxt;
        private TextView timeTxt;
        private JCVideoPlayerStandard videoPlayer;

        public ViewHolder(View itemView) {
            super(itemView);
            titleTxt = itemView.findViewById(R.id.item_forum_title_txt);
            videoImgLayout = itemView.findViewById(R.id.item_forum_video_img_layout);
            smallImgLayout = itemView.findViewById(R.id.item_forum_small_picture);
            bigImg = itemView.findViewById(R.id.item_forum_big_picture);
            scanTxt = itemView.findViewById(R.id.item_forum_scan_sum_txt);
            replyTxt = itemView.findViewById(R.id.item_forum_reply_sum_txt);
            timeTxt = itemView.findViewById(R.id.item_forum_publish_time_txt);
            videoPlayer = itemView.findViewById(R.id.item_forum_video_player);
        }
    }

    public void setOnItemRecyClickListener(OnItemRecyClickListener mListener) {
        this.mListener = mListener;
    }
}
